var rememberChecked = true;

$(document).ready(function(){
	$("#merchant_id").val(getCookie("merchant_id"));
	
	$("#merchant_id").focus(function(){
		$("#merchant_id").css("background", "url('" + contextPath + "/image/bg_text_blank.png')");
	});
	$("#merchant_id").blur(function(){
		if ($("#merchant_id").val() == "") {
			$("#merchant_id").css("background", "url('" + contextPath + "/image/bg_text_uid.png')");
		}
	});
	

	$("#merchant_password").focus(function(){
		$("#merchant_password").css("background", "url('" + contextPath + "/image/bg_text_blank.png')");
	});
	$("#merchant_password").blur(function(){
		if ($("#merchant_password").val() == "") {
			$("#merchant_password").css("background", "url('" + contextPath + "/image/bg_text_password.png')");
		}
	});
	
	$("#merchant_id").focus();
	addFadeEffect();
});

function toggleRememberRadio() {
	rememberChecked = !rememberChecked;
	
	if (rememberChecked) {
		$("#remember_user").css("background", "url('" + contextPath + "/image/radio_gray_selected.png')");
	} else {
		$("#remember_user").css("background", "url('" + contextPath + "/image/radio_gray_unselected.png')");
	}
}

function cancelMerchantLogin() {
	$("#div_merchant_login").remove();
	$(cover).remove();
}

function merchantLogin() {
	var merchantId = $("#merchant_id").val();
	var password = $("#merchant_password").val();
	var pattern = /[a-zA-Z0-9]{4,20}/;
	var requestUrl = contextPath + "/merchant/login";
	var params;
	
	if (merchantId == "" || !pattern.test(merchantId)) {
		$("#error_message_login").html("用户名错误<br/>");
		$("#merchant_id").css("background", "#fbe2e2");
		return;
	} else {
		$("#error_message_login").html("");
		$("#merchant_id").css("background", "url('" + contextPath + "/image/bg_text_blank.png')");
	}
	
	if (password == "" || !pattern.test(password)) {
		$("#error_message_login").html("密码错误<br/>");
		$("#merchant_password").css("background", "#fbe2e2");
		return;
	} else {
		$("#error_message_login").html("");
		$("#merchant_password").css("background", "url('" + contextPath + "/image/bg_text_blank.png')");
	}
	
	params = {
			merchant_id:merchantId,
			password:password
	};
	
	$.post(requestUrl, params, function(ret){
		if (ret.code == 0) {
			if (rememberChecked) {
				setCookie("merchant_id", merchantId);
			} else {
				deleteCookie("merchant_id");
			}
			window.location.href = contextPath + "/merchant/top.jsp";
		} else {
			$("#error_message_login").html(ret.message);
		}
	}, "json");
}